import java.util.concurrent.*;

public class saleTicketPlus {

    public static void main(String[] args) {

        ExecutorService executor = Executors.newFixedThreadPool(3);
        Ticket ticket = new Ticket();

        try {
            for (int i = 0; i < 50; i++)
                executor.execute(() -> {
                    ticket.saleTicket();
                });
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            executor.shutdown();
        }
    }

}

class saleTicketPlusThreadPool {

    public static void main(String[] args) {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,//核心线程数
                6,//最大线程数
                60,//空闲线程存活时间
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(50),//任务队列上限
                Executors.defaultThreadFactory(),//创建线程工厂
                new ThreadPoolExecutor.AbortPolicy()//拒绝任务策略
        );

        Ticket ticket = new Ticket();
        for (int i = 0; i < 50; i++)
            threadPoolExecutor.execute(() -> {
                ticket.saleTicket();
            });
        threadPoolExecutor.shutdown();
    }

}
